﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace SystemHelperLibrary.API.Ini
{
	public sealed class IniHelper
	{
		[DllImport("Kernel32.dll")]
		internal static extern uint GetPrivateProfileInt(string sectionName, string keyName, int defVal, string fName);

		[DllImport("Kernel32.dll")]
		internal static extern uint GetPrivateProfileSection(string sectionName, StringBuilder buff, int bufLen, string fName);

		[DllImport("Kernel32.dll")]
		internal static extern uint GetPrivateProfileSectionNames(StringBuilder buff, int bufLen, string fName);

		[DllImport("Kernel32.dll")]
		internal static extern uint GetPrivateProfileString(string sectionName, string keyName, string defVal, StringBuilder buffer,
															int bufLen, string fileName);

		[DllImport("Kernel32.dll")]
		internal static extern bool GetPrivateProfileStruct(string sectionName, string keyName, IntPtr lpStruct, uint uSizeStruct,
															string fileName);

		[DllImport("Kernel32.dll")]
		internal static extern bool WritePrivateProfileSection(string sectionName, string lpSectionData, string fileName);

		[DllImport("Kernel32.dll")]
		internal static extern bool WritePrivateProfileString(string sectionName, string lpKeyName, string keyData, string fileName);

		[DllImport("Kernel32.dll")]
		internal static extern bool WritePrivateProfileStruct(string sectionName, string lpKeyName, IntPtr lpStruct, uint size, string fileName);

		public static string GetPrivateProfileSectionApi(string section, string fname)
		{
			int bufLen = 256;
			StringBuilder sb = new StringBuilder(bufLen);
			uint rez = GetPrivateProfileSection(section, sb, bufLen, fname);
			return (sb == null ? string.Empty : sb.ToString());
		}

		public static string GetSectionsNames(string fname)
		{
			int bufLen = 1024;
			StringBuilder sb = new StringBuilder(bufLen);
			uint rez = GetPrivateProfileSectionNames(sb, bufLen, fname);
			return sb.ToString();
		}

		public static bool WriteSection(string fname, string secName, string secData)
		{
			if (!System.IO.File.Exists(fname))
			{
				using (var fs = System.IO.File.Create(fname))
				{
				}
			}
			else
				System.IO.File.Delete(fname);

			return WritePrivateProfileSection(secName, secData, fname);
		}

		public static bool WriteValue(string fname, string secName, string keyName, string keyData)
		{
			if (!System.IO.File.Exists(fname))
				return false;

			return WritePrivateProfileString(secName, keyName, keyData, fname);
		}

		public static string GetValue(string fname, string secName, string keyName)
		{
			if (!System.IO.File.Exists(fname))
				return string.Empty;

			int len = 64;
			StringBuilder sb = new StringBuilder(len);
			uint rez = GetPrivateProfileString(secName, keyName, string.Empty, sb, len, fname);
			return sb.ToString();
		}

		public static string GetIniStruct(string fname, string secName, string keyName)
		{
			IntPtr pStr = IntPtr.Zero;
			uint len = 12;
			bool rez = GetPrivateProfileStruct(secName, keyName, pStr, len, fname);
			return Marshal.PtrToStringUni(pStr);
		}

	}
}
